package com.itheima.health.service.impl;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.itheima.health.common.RedisConst;
import com.itheima.health.service.SmsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;

import java.security.SecureRandom;
import java.util.concurrent.TimeUnit;


@DubboService
@Slf4j
public class SmsServiceImpl implements SmsService {

    @Autowired
    RedisTemplate redisTemplate;
    //阿里云自己的asId和askey
    private IAcsClient acsClient = new DefaultAcsClient(DefaultProfile.getProfile("cn-hangzhou", "LTAI4GDg5TWtzUXTyS6BE3Bk", "2lnEx7fWsml9li2smCqiMymWHmz5ux"));
    /** 
     * @Author: JASON
     * @Date: 15:57 2021/1/10
     * @Parms [type, telephone]
     * @ReturnType: void
     * @Description: 发送短信验证码并存到redis中
    */
    @Override
    public void sendValidateCode(String type, String telephone) {
        log.info("[验证码-发送]type:{},telephone:{}", type, telephone);
        //生成验证码
        int code = (int) (1000 + new SecureRandom().nextDouble() * 8999);
        log.info("随机生成的验证码为"+code);
        //存储到redis中   key格式: 前缀+类型+手机号
        String redisKey = RedisConst.VALIDATA_CODE_PREFILX + type+telephone;
        //存储 并设置最大时间  300秒
        redisTemplate.boundValueOps(redisKey).set(String.valueOf(code),5*60,TimeUnit.SECONDS);
        //调用短息服务给用户发送验证码
         //构造请求参数
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", telephone);
        request.putQueryParameter("SignName", "传智健康项目");
        request.putQueryParameter("TemplateCode", "SMS_175060789");
        request.putQueryParameter("TemplateParam", "	{\"assertCode\":\"" + code + "\"}");
        try {
            //发起请求
            CommonResponse response = acsClient.getCommonResponse(request);
            log.info("[短信-发送验证码]response:{}", response.getData());
        } catch (ClientException e) {
            log.error("[短信-发送验证码]", e);
        }
    }

    /**
     * @Author: JASON
     * @Date: 15:57 2021/1/10
     * @Parms [type, telephone, code]
     * @ReturnType: boolean
     * @Description: 校验验证码
    */
    @Override
    public boolean checkValidateCode(String type,String telephone,String code){
        log.info("[短信-校验验证码]type:{},phone:{},code:{}", type, telephone, code);
        String redisKey = RedisConst.VALIDATA_CODE_PREFILX + code + telephone;
        //从redis中取出code
        String redisCode = (String) redisTemplate.boundValueOps(redisKey).get();
        if(!StringUtils.isEmpty(redisCode)&&code.equals(redisCode)){
            return true;
        }
        return false;
    }
}
